package proj.server;

import java.beans.*;
import java.io.*;
import java.lang.reflect.*;
import java.net.*;
import java.util.*;

import proj.common.*;
import proj.novel.*;

//线程
public class ServerThread extends Thread {
	private Socket s;

	public ServerThread(Socket s) {
		this.s = s;
	}

	@Override
	public void run() {
		try {
			System.out.println(s + "连接了。。。");
			OutputStream os = s.getOutputStream();
			InputStream is = s.getInputStream();
			ObjectOutputStream oos = new ObjectOutputStream(os);
			ObjectInputStream ois = new ObjectInputStream(is);
			while (true) {
				Request req = (Request) ois.readObject();
				Response res = null;
				Method m = this.getClass().getDeclaredMethod(req.getAction(), Request.class);
				try {
					res = (Response) m.invoke(this, req);
				} catch (Exception e) {
					res = new Response();
					res.setSucess(false);
					res.setMsg("出现异常：");
					e.printStackTrace();
				}
				oos.writeObject(res);
				System.out.println(res.getMsg());
				oos.flush();
				oos.reset();
				if (req.getAction().equals("quit"))
					return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			System.out.println(s + "断开连接");
		}
	}

	// 用户登录
	public Response login(Request req) {
		String name = (String) req.get("userName");
		String pass = (String) req.get("userPass");
		Response res = null;
		XMLDecoder xd = null;
		try {
			xd = new XMLDecoder(new FileInputStream("src/proj/user/user.xml"));
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) xd.readObject();
			if (map.containsKey(name)) {
				System.out.println(map.get(name));
				if (map.get(name).equals(pass))
					res = new Response(true, "登陆成功");
				else
					res = new Response(false, "密码错误！");
			} else {
				res = new Response(false, "用户不存在！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (xd != null)
				xd.close();
		}
		return res;
	}

	// 用户注册
	public Response register(Request req) {
		Response res = null;
		String name = (String) req.get("userName");
		String pass = (String) req.get("userPass");
		XMLDecoder xd = null;
		XMLEncoder xe = null;
		try {
			xd = new XMLDecoder(new FileInputStream("src/proj/user/user.xml"));
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) xd.readObject();
			if (map.containsKey(name))
				res = new Response(false, "该用户已存在！");
			else {
				map.put(name, pass);
				xe = new XMLEncoder(new FileOutputStream("src/proj/user/user.xml"));
				xe.writeObject(map);
				xe.flush();
				res = new Response(true, "注册成功！");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (xd != null)
				xd.close();
			if (xe != null)
				xe.close();
		}
		return res;
	}

	// 用户修改密码
	public Response changePass(Request req) {
		Response res = null;
		String name = (String) req.get("userName");
		String newpass = (String) req.get("userPass");
		XMLDecoder xd = null;
		XMLEncoder xe = null;
		try {
			xd = new XMLDecoder(new FileInputStream("src/proj/user/user.xml"));
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) xd.readObject();
			map.put(name, newpass);
			xe = new XMLEncoder(new FileOutputStream("src/proj/user/user.xml"));
			xe.writeObject(map);
			xe.flush();
			res = new Response(true, "修改成功！");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (xd != null)
				xd.close();
			if (xe != null)
				xe.close();
		}
		return res;
	}

	// 退出系统
	public Response quit(Request req) {
		return new Response(true, "退出");
	}

	// 下载小说
	public Response getContent(Request req) {
		Response res = null;
		String name = (String) req.get("filename");
		File f = new File("F:/download", name + ".txt");
		if (!f.exists())
			return res = new Response(false, "文件不存在！");
		byte[] b = new byte[(int) f.length()];
		try {
			FileInputStream fis = new FileInputStream(f);
			fis.read(b);
			res = new Response(true, name);
			res.put("content", b);
			return res;
		} catch (Exception e) {
			e.printStackTrace();
			return res = new Response(false, "失败");
		}
	}

	// 上传小说
	public Response upload(Request req) {
		Response res = null;
		XMLEncoder xe = null;
		try {
			List<Novel> list = getNovel();
			Novel newnovel = (Novel) req.get("novel");
			byte[] b = (byte[]) req.get("fileContent");
			if (list.contains(newnovel)) {
				return new Response(false, "文件已存在");
			}
			FileOutputStream out = new FileOutputStream("F:/upload" + newnovel.getName() + ".txt");
			out.write(b);
			out.close();
			newnovel.setNo(list.size() + 1);
			list.add(newnovel);
			writeXml(list);
			res = new Response(true, "小说" + newnovel.getName() + "上传成功！");
		} catch (Exception e) {
			e.printStackTrace();
			res.setSucess(false);
			res.setMsg("小说上传失败：" + e);
		}
		return res;
	}

	// 展示所有小说信息
	public Response fileIntro(Request req) {
		Response res = null;
		try {
			XMLDecoder xd = new XMLDecoder(new FileInputStream("src/proj/novel/novelIntro.xml"));
			@SuppressWarnings("unchecked")
			List<Novel> list = (List<Novel>) xd.readObject();
			res = new Response(true, "小说信息");
			res.put("fileInfo", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	// 获取小说信息
	public List<Novel> getNovel() {
		XMLDecoder xd = null;
		try {
			xd = new XMLDecoder(new FileInputStream("src/proj/novel/novelIntro.xml"));
		} catch (Exception e) {
			e.printStackTrace();
		}
		@SuppressWarnings("unchecked")
		List<Novel> list = (List<Novel>) xd.readObject();
		return list;
	}

	// 将小说信息写入xml
	public void writeXml(List<Novel> list) {
		try {
			XMLEncoder xe = new XMLEncoder(new FileOutputStream("src/proj/novel/novelIntro.xml"));
			xe.writeObject(list);
			xe.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
